var focusProducts = {};
focusProducts.methods = {};
focusProducts.methods.bookClass = "1";
$(function () {

    // 分类更多
    focusProducts.methods.focusProductsMore();

    // 关注商品 切换    
    focusProducts.methods.focusProductsTab();

    // 多选及批量
    focusProducts.methods.allLiDelete();

    // 多选及批量状态
    focusProducts.methods.chooseLiImg();

    // 是否取消关注
    focusProducts.methods.isCancelFocus();

    //样式状态
    focusProducts.methods.allLiImg();

    // 区域显隐
    focusProducts.methods.cascadeShowHide();

    //  级联
     focusProducts.methods.cascade();

    // 分页
    var page = new Controller.PageControl();
    page.init({
        totalCount: 100, //总页数
        conter: ".shop-page", //容器选择器
        nowPage: 1 //当前页，默认第几页
        // callback:fun,      
        // gotoCallBack:fun   
    });

})


// 分类更多
focusProducts.methods.focusProductsMore = function () {
    if ($(".shop-user-focusProduct-cut ul").height() <= $(".shop-user-focusProduct-cut").height()) {
        $(".shop-user-focusProduct-cut .shop-focusProduct-more").hide();
    } else {
        $(".shop-user-focusProduct-cut .shop-focusProduct-more").show();
    }
    $(".shop-user-focusProduct-cut").on("click", ".shop-focusProduct-more", function () {
        if ($(this).hasClass("shop-focusProduct-more-top")) {
            $(this).removeClass("shop-focusProduct-more-top");
            $(this).text("更多");
            $(this).parent(".shop-user-focusProduct-cut").css("height", "48px");
        } else {
            $(this).addClass("shop-focusProduct-more-top");
            $(this).text("收起");
            $(this).parent(".shop-user-focusProduct-cut").height($(".shop-user-focusProduct-cut ul").height());
        }
    })
}

// 关注商品 切换 
focusProducts.methods.focusProductsTab = function () {
    $(".shop-user-focusProduct-cut li").on("click", function () {
        focusProducts.methods.allLiImg();
        $(this).addClass("shop-click").siblings().removeClass("shop-click");
        $(".shop-user-focusProduct-con-list").hide();
        focusProducts.methods.bookClass = $(this).attr("shop-book-class");
        $(".shop-user-focusProduct-con-list[shop-book-class = " + focusProducts.methods.bookClass + "]").show();
    })
}

// 多选及批量
focusProducts.methods.allLiDelete = function () {
    $(".shop-user-focusProduct-completeBtn").on("click", function () {
        $(".shop-focusProduct-allManagement").show();
        $(".shop-user-focusProduct-sel-complete").hide();
        $(".shop-user-focusProduct-con-list-cornerBG").hide();
        $(".shop-user-focusProduct-con-list-mark").hide();
        // $(".shop-user-focusProduct-cancelFocus").removeClass("shop-user-focusProduct-cancelFocus-red");
        $(".shop-user-focusProduct-con-list[shop-book-class=" + focusProducts.methods.bookClass + "]").find("ul > li").each(function () {
            if ($(this).find(".shop-user-focusProduct-con-list-mark").hasClass("shop-user-focusProduct-con-list-mark-red") && $(".shop-user-focusProduct-cancelFocus").hasClass("shop-user-focusProduct-cancelFocus-red")) {
                var bookIndexArr = $(this).attr("shop-book-id");
                $(".shop-user-focusProduct-con-list li[shop-book-id = " + bookIndexArr + "]").remove();
            }
        })

        if ($(".shop-user-focusProduct-con-list[shop-book-class=" + focusProducts.methods.bookClass + "]").find("li").length == 0) {
            $(".shop-user-focusProduct-sel-all input").attr("checked", false);
        }

        $(".shop-user-focusProduct-cancelFocus").removeClass("shop-user-focusProduct-cancelFocus-red");
        $(".shop-user-focusProduct-car").removeClass("shop-user-focusProduct-car-red");
    })
}

// 多选及批量状态
focusProducts.methods.chooseLiImg = function () {
    $(".shop-user-focusProduct-con-list li").live("click", function () {
        var bookId = $(this).attr("shop-book-id");
        if ($(this).find(".shop-user-focusProduct-con-list-cornerBG").css("display") == "none") {
            return;
        } else {
            $(".shop-user-focusProduct-con-list li[shop-book-id = " + bookId + "]").find(".shop-user-focusProduct-con-list-mark").toggleClass("shop-user-focusProduct-con-list-mark-red");
            var len = $(this).parents(".shop-user-focusProduct-con-list").find("li").length;
            // 用选中的长度和书的长度判断是否全选
            if ($(this).parents(".shop-user-focusProduct-con-list").find(".shop-user-focusProduct-con-list-mark-red").length == len) {
                $(".shop-user-focusProduct-sel-all input").attr("checked", true);
            } else {
                $(".shop-user-focusProduct-sel-all input").attr("checked", false);
            }
        }
    })
}

// 是否取消关注
focusProducts.methods.isCancelFocus = function () {
    $(".shop-user-focusProduct-cancelFocus").on("click", function () {
        $(this).toggleClass("shop-user-focusProduct-cancelFocus-red");
    })

    $(".shop-user-focusProduct-car").on("click",function(){
        $(this).toggleClass("shop-user-focusProduct-car-red");
    })
    
}


//样式状态
focusProducts.methods.allLiImg = function () {
    // 初始状态
    $(".shop-focusProduct-allManagement").show();
    $(".shop-user-focusProduct-address").show();
    $(".shop-user-focusProduct-sel-complete").hide();
    $(".shop-user-focusProduct-con-list-cornerBG").hide();
    $(".shop-user-focusProduct-con-list-mark").hide();
    $(".shop-user-focusProduct-sel-all input").attr("checked", false);
    $(".shop-user-focusProduct-con-list-mark").removeClass("shop-user-focusProduct-con-list-mark-red");
    $(".shop-user-focusProduct-cancelFocus").removeClass("shop-user-focusProduct-cancelFocus-red");
    $(".shop-user-focusProduct-car").removeClass("shop-user-focusProduct-car-red");

    $(".shop-focusProduct-allManagement").on("click", function () {
        $(".shop-focusProduct-allManagement").hide();
        $(".shop-user-focusProduct-sel-complete").show();
        $(".shop-user-focusProduct-con-list-cornerBG").show();
        $(".shop-user-focusProduct-con-list-mark").show();
    })

    $(".shop-user-focusProduct-con li").live("mouseover", function () {
        if ($(this).find(".shop-user-focusProduct-con-list-cornerBG").css("display") == "none") {
            $(this).find(".shop-focusProduct-similar").show();
            $(this).find(".shop-focusProduct-collection").show();
        } else {
            $(this).find(".shop-focusProduct-similar").hide();
            $(this).find(".shop-focusProduct-collection").hide();
        }
    }).live("mouseout", function () {
        $(this).find(".shop-focusProduct-similar").hide();
        $(this).find(".shop-focusProduct-collection").hide();
    });

    $(".shop-user-focusProduct-sel-all input").change(function () {
        if ($(".shop-user-focusProduct-sel-all input").attr("checked")) {
            $(".shop-user-focusProduct-con-list[shop-book-class=" + focusProducts.methods.bookClass + "]").find(".shop-user-focusProduct-con-list-mark").addClass("shop-user-focusProduct-con-list-mark-red");
        } else {
            $(".shop-user-focusProduct-con-list[shop-book-class=" + focusProducts.methods.bookClass + "]").find(".shop-user-focusProduct-con-list-mark").removeClass("shop-user-focusProduct-con-list-mark-red");
        }
    })
}






focusProducts.province = null //省
focusProducts.city = null //市
focusProducts.area = null //区
focusProducts.areas = [];
// 区域显隐
focusProducts.methods.cascadeShowHide = function () {
    $(".shop-user-focusProduct-address-con").on("click", ".shop-focusProduct-delete", function () {
        $(".shop-user-focusProduct-address-con").hide();
    })

    $(".shop-user-focusProduct-address").hover(function () {
        $(".shop-user-focusProduct-address-con").show();
        $(".shop-user-focusProduct-address-title").css("border-bottom", "1px #fff solid");
    }, function () {
        $(".shop-user-focusProduct-address-con").hide();
        $(".shop-user-focusProduct-address-title").css("border-bottom", "1px #eee solid");
    })
}


focusProducts.methods.cascade = function () {
    //获取城市信息
    $.get("/Metadata/GetTreeAreasAsync", {
    }, function (data) {
        focusProducts.methods.levelSwitching(); //省市区选择区域切换
        $(".shop-focusProduct-group").html(""); //清空头部选择区域
        focusProducts.areas = data;
        console.log(data)
        focusProducts.methods.AreaInitialization();
        focusProducts.methods.levelSwitchingThis(0);
        $(".shop-focusProduct-cascad-main").eq(0).on("click", "li", focusProducts.methods.chooseProvince); // 省选择事件绑定
        $(".shop-focusProduct-cascad-main").eq(1).on("click", "li", focusProducts.methods.chooseCity); // 市选择事件绑定
        $(".shop-focusProduct-cascad-main").eq(2).on("click", "li", focusProducts.methods.chooseArea); // 区选择事件绑定
     });
}

// 省选择事件响应函数
focusProducts.methods.chooseProvince = function () {
    var index = $(this).find('span').attr('data-value');
    for (var i = 0; i < focusProducts.areas.length; i++) {
        if (focusProducts.areas[i].id == index) {
            focusProducts.province = focusProducts.areas[i];
        }
    }
    focusProducts.city = null;
    focusProducts.area = null;
    focusProducts.methods.AreaInitialization();
    focusProducts.methods.levelSwitchingThis(1);
    if (focusProducts.province.children.length == 0) {
        $(".shop-user-focusProduct-address-title").html(focusProducts.province["name"] + "<i></i>");
        $(".shop-user-focusProduct-address-con").hide();
        $(".shop-focusProduct-group li").eq(0).show().siblings().hide();
        $(".shop-focusProduct-group li").eq(0).addClass("shop-focusProduct-cascad-item-red").siblings("li").removeClass("shop-focusProduct-cascad-item-red");
        $(".shop-focusProduct-cascads > ol").eq(0).show().siblings().hide();
        $(".shop-user-focusProduct-address-title").css("border-bottom", "1px #eee solid");
    }

}
//市选择事件响应函数
focusProducts.methods.chooseCity = function () {
    var index = $(this).find('span').attr('data-value');
    for (var i = 0; i < focusProducts.province.children.length; i++) {
        if (focusProducts.province.children[i].id == index) {
            focusProducts.city = focusProducts.province.children[i];
        }
    }
    focusProducts.area = null;
    focusProducts.methods.AreaInitialization();
    focusProducts.methods.levelSwitchingThis(2);
    if (focusProducts.city.children.length == 0) {
        $(".shop-user-focusProduct-address-title").html(focusProducts.province["name"] + focusProducts.city["name"] + "<i></i>");
        $(".shop-user-focusProduct-address-con").hide();
        $(".shop-focusProduct-group li").eq(2).hide();
        $(".shop-focusProduct-group li").eq(1).addClass("shop-focusProduct-cascad-item-red").siblings("li").removeClass("shop-focusProduct-cascad-item-red");
        $(".shop-focusProduct-cascads > ol").eq(1).show().siblings().hide();
        $(".shop-user-focusProduct-address-title").css("border-bottom", "1px #eee solid");
    }
}
// 区选择事件响应函数
focusProducts.methods.chooseArea = function () {
    var index = $(this).find('span').attr('data-value');
    for (var i = 0; i < focusProducts.city.children.length; i++) {
        if (focusProducts.city.children[i].id == index) {
            focusProducts.area = focusProducts.city.children[i];
        }
    }
    focusProducts.methods.AreaInitialization();
    focusProducts.methods.levelSwitchingThis(2);
    $(".shop-user-focusProduct-address-title").html(focusProducts.province["name"] + focusProducts.city["name"] + focusProducts.area["name"] + "<i></i>");
    $(".shop-user-focusProduct-address-con").hide();
    $(".shop-user-focusProduct-address-title").css("border-bottom", "1px #eee solid");
}


// 数据更新
focusProducts.methods.AreaInitialization = function () {
    $(".shop-focusProduct-group").text("");
   
    focusProducts.methods.selectionAreaFilling(focusProducts.areas, 0);
    if (focusProducts.province != null) {
        focusProducts.methods.dataFilling(focusProducts.province);
        focusProducts.methods.selectionAreaFilling(focusProducts.province.children, 1);
    } else {
        focusProducts.methods.dataFilling({ text: "请选择" });
        return
    }
    if (focusProducts.city != null) {
        focusProducts.methods.dataFilling(focusProducts.city);
        focusProducts.methods.selectionAreaFilling(focusProducts.city.children, 2);
    } else {
        focusProducts.methods.dataFilling({ text: "请选择" });
        return
    }
    if (focusProducts.area != null) {
        focusProducts.methods.dataFilling(focusProducts.area);
    } else {
        focusProducts.methods.dataFilling({ text: "请选择" });
        return
    }
}

// 省市区数据填充
focusProducts.methods.dataFilling = function (data) {
    $(".shop-focusProduct-group").append("<li class='shop-focusProduct-cascad-item'><span>" + data.text + "</span></li>")
}

// 选择区数据填充
focusProducts.methods.selectionAreaFilling = function (data, index) {
    $(".shop-focusProduct-cascad-main").eq(index).text("");
    for (var i = 0, len = data.length; i < len; i++) {
        $(".shop-focusProduct-cascad-main").eq(index).append("<li><span data-value='" + data[i]["id"] + "'>" + data[i].name + "</span></li>")
    }
}


// 当前正在选择的[省/市/区]
focusProducts.methods.levelSwitching = function () {
    
    $(".shop-focusProduct-group").on("click", "li", function () {
        var index = $(this).index();
        focusProducts.methods.levelSwitchingThis(index);
    });
}
// 指定正在选择的[省/市/区]
focusProducts.methods.levelSwitchingThis = function (index) {
    $(".shop-focusProduct-group li").eq(index).addClass("shop-focusProduct-cascad-item-red").siblings("li").removeClass("shop-focusProduct-cascad-item-red");
    $(".shop-focusProduct-cascads > ol").eq(index).show().siblings("ol").hide();
} 
